package com.beans;

import com.classe.Rotas;
import com.dao.RotasDAO;
import com.utils.CoordinateConversion;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
import org.primefaces.model.map.Polyline;
import org.primefaces.model.TreeNode;

public class RotasBean implements Serializable {

    private MapModel advancedModel;
    private Marker marker;
    private TreeNode root;
    private TreeNode[] selectedNodes;
    ArrayList<Rotas> lista = RotasDAO.listar("");

    public RotasBean() {

        advancedModel = new DefaultMapModel();

        Polyline polyline = new Polyline();
        CoordinateConversion cc = new CoordinateConversion();

        String corAnterior = null;
        String cor = null;
        Marker ultimoMaker = null;


        for (int i = 0; i < lista.size(); i++) {

            Rotas rotas = lista.get(i);
            double coordx;
            double coordy;

            String convertida = cc.utm2LatLon("22 J " + rotas.getCoordx() + " " + rotas.getCoordy());
            String[] utm = convertida.split(";");
            coordx = Double.parseDouble(utm[0]);
            coordy = Double.parseDouble(utm[1]);

            LatLng coord1 = new LatLng(coordx, coordy);

            root = new DefaultTreeNode("Root", null);
            TreeNode node0 = new DefaultTreeNode(rotas.getRota(), root);
            TreeNode node00 = new DefaultTreeNode(rotas.getRota(), node0);  


            if (corAnterior == null || !corAnterior.equals(rotas.getCor())) {
                corAnterior = rotas.getCor();
                cor = rotas.getCor();
                polyline = new Polyline();
                polyline.setStrokeWeight(5);
                polyline.setStrokeColor(rotas.getCor());
                polyline.setStrokeOpacity(1);
                advancedModel.addOverlay(polyline);
                advancedModel.addOverlay(new Marker(coord1, "DADOS: \nUC: " + rotas.getUC() + "\nRota: " + rotas.getRota() + "\nSequência: " + rotas.getSequencia() + "\nLigado: "
                        + rotas.getLigado(), null, "http://www.googlemapsmarkers.com/v1/I/" + rotas.getCor()));

                if (ultimoMaker != null) {
                    //ultimoMaker.setIcon("http://www.googlemapsmarkers.com/v1/F/" + cor);
                }
            } else {
                ultimoMaker = new Marker(coord1, "DADOS: \nUC: " + rotas.getUC() + "\nRota: "
                        + rotas.getRota() + "\nSequência: " + rotas.getSequencia() + "\nLigado: " + rotas.getLigado(), null,
                        "http://www.googlemapsmarkers.com/v1/" + rotas.getCor());

                advancedModel.addOverlay(ultimoMaker);
            }

            polyline.getPaths().add(coord1);

        }
        //ultimoMaker.setTitle("(F) " + ultimoMaker.getTitle());
        // ultimoMaker.setIcon("http://www.googlemapsmarkers.com/v1/F/" + cor);
    }

     public TreeNode getRoot() {  
        return root;  
    }  
  
    public TreeNode[] getSelectedNodes() {  
        return selectedNodes;  
    }  
  
    public void setSelectedNodes(TreeNode[] selectedNodes) {  
        this.selectedNodes = selectedNodes;  
    }  
  
    public void displaySelectedMultiple(ActionEvent event) {  
        if(selectedNodes != null && selectedNodes.length > 0) {  
            StringBuilder builder = new StringBuilder();  
  
            for(TreeNode node : selectedNodes) {  
                builder.append(node.getData().toString());  
                builder.append("<br />");  
            }  
  
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());  
  
            FacesContext.getCurrentInstance().addMessage(null, message);  
        }
    }
    
    
    public ArrayList<Rotas> getLista() {
        return lista;
    }

    public void setLista(ArrayList<Rotas> lista) {
        this.lista = lista;
    }

    public MapModel getAdvancedModel() {
        return advancedModel;
    }

    public void onMarkerSelect(OverlaySelectEvent event) {
        marker = (Marker) event.getOverlay();
    }

    public Marker getMarker() {
        return marker;
    }
}
